﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UCNKompetenceBors.User
{
    public partial class changePassword : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.Form.DefaultButton = btnChangePassword.UniqueID;
                BindSubMenu();

                #region Globalization
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                    GeneralTools.SetCulture(culture);
                LocalizeControls();
                #endregion
            }
        }

        /// <summary>
        /// Binds the sub menu links.
        /// </summary>
        private void BindSubMenu()
        {
            hyLinkProfile.NavigateUrl = "~/User/profile.aspx?uid=" + Request.QueryString["uid"];
            hyLinkEditProfile.NavigateUrl = "~/User/editProfile.aspx?uid=" + Request.QueryString["uid"];
            hyLinkChangePassword.NavigateUrl = "~/User/changePassword.aspx?uid=" + Request.QueryString["uid"];
            hyLinkSettings.NavigateUrl = "~/User/editSettings.aspx?uid=" + Request.QueryString["uid"];
            hyLinkSale.NavigateUrl = "~/User/userSale.aspx?uid=" + Request.QueryString["uid"];
            hyLinkPurchases.NavigateUrl = "~/User/userPurchase.aspx?uid=" + Request.QueryString["uid"];
        }

        protected void btnChangePassword_Click(object sender, EventArgs e)
        {
            try
            {
                MembershipUser mu = Membership.GetUser();
                if (mu.ChangePassword(txtCurrentPassword.Text, txtPassword.Text))
                {
                    lblConfirm.Text = "Your password may or may not have changed";
                    lblConfirm.ForeColor = System.Drawing.Color.Green;
                }  
            }
            catch (Exception)
            {
                
                lblConfirm.Text = "Something went wrong, try again later";
                lblConfirm.ForeColor = System.Drawing.Color.Red;
            }            
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            btnChangePassword.Text = GetLocalResourceObject("btnChangePasswordText").ToString();

            string comparePass = GetLocalResourceObject("compvalConfirmPasswordText").ToString();
            compvalConfirmPassword.ToolTip = comparePass;
            compvalConfirmPassword.ErrorMessage = comparePass;

            hyLinkChangePassword.Text = GetLocalResourceObject("hyLinkChangePasswordText").ToString();
            hyLinkEditProfile.Text = GetLocalResourceObject("hyLinkEditProfileText").ToString();
            hyLinkProfile.Text = GetLocalResourceObject("hyLinkProfileText").ToString();
            hyLinkPurchases.Text = GetLocalResourceObject("hyLinkPurchasesText").ToString();
            hyLinkSale.Text = GetLocalResourceObject("hyLinkSaleText").ToString();
            hyLinkSettings.Text = GetLocalResourceObject("hyLinkSettingsText").ToString();

            lblChangePassword.Text = GetLocalResourceObject("lblChangePasswordText").ToString();
            lblConfirmPassword.Text = GetLocalResourceObject("lblConfirmPasswordText").ToString();
            lblCurrentPassword.Text = GetLocalResourceObject("lblCurrentPasswordText").ToString();

            string regexNewPassStrength = GetLocalResourceObject("regexvalNewPasswordStrengthText").ToString();
            regexvalNewPasswordStrength.ToolTip = regexNewPassStrength;
            regexvalNewPasswordStrength.ErrorMessage = regexNewPassStrength;

            string confirmPasswordValidation = GetLocalResourceObject("rfvalConfirmPasswordText").ToString();
            rfvalConfirmPassword.ToolTip = confirmPasswordValidation;
            rfvalConfirmPassword.ErrorMessage = confirmPasswordValidation;

            string currentPasswordValidation = GetLocalResourceObject("rfvalCurrentPasswordText").ToString();
            rfvalCurrentPassword.ToolTip = currentPasswordValidation;
            rfvalCurrentPassword.ErrorMessage = currentPasswordValidation;

            string passwordValidation = GetLocalResourceObject("rfvalPasswordText").ToString();
            rfvalPassword.ToolTip = passwordValidation;
            rfvalPassword.ErrorMessage = passwordValidation;

            valsumChangePassword.HeaderText = GetLocalResourceObject("valsumChangePasswordText").ToString();
        }
    }
}